﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RuoVea.GoFastdfs.Common
{
    /// <summary>
    /// 日期操作工具类
    /// </summary>
    public class DateUtil
    {
        /// <summary>
        /// 日期格式化
        /// </summary>
        /// <param name="format"></param>
        /// <returns></returns>
        public static String getFormatDate(String format)
        {
            if (string.IsNullOrEmpty(format))
                format = "yyyy-MM-dd HH:mm:ss";
            return new DateTime().ToString(format);
        }
        /// <summary>
        /// 日期格式化
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static String getFormatDate(DateTime date)
        {
            return date.ToString("yyyy-MM-dd HH:mm:ss");
        }
        /// <summary>
        /// 日期格式化
        /// </summary>
        /// <param name="date"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static String getFormatDate(DateTime date, String format)
        {
            if (string.IsNullOrEmpty(format))
                format = "yyyy-MM-dd HH:mm:ss";
            return date.ToString(format);
        }

        /// <summary>
        /// 字符串转换成日期
        /// </summary>
        /// <param name="str">格式为yyyy-MM-dd HH:mm:ss</param>
        /// <returns></returns>
        public static DateTime StrToDate(String str)
        {
            DateTime date = new DateTime();
            try
            {
                date = DateTime.ParseExact(str, "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture);
            }
            catch (Exception e)
            {
                throw e;// e.StackTrace();
            }
            return date;
        }
        /**
		* 字符串转换成日期
		* @param str 格式为yyyy-MM-dd HH:mm:ss   
		* @return date
		*/
        public static DateTime StrToDate(String str, String format)
        {
            if (string.IsNullOrEmpty(format))
                format = "yyyy-MM-dd HH:mm:ss";
            DateTime date = new DateTime();
            try
            {
                date = DateTime.ParseExact(str, format, System.Globalization.CultureInfo.CurrentCulture);
            }
            catch (Exception e)
            {
                throw e;
            }
            return date;
        }

        /// <summary>
        /// 获得间隔后的格式化后的时间
        /// </summary>
        /// <param name="intervalTime">时间间隔 单位秒</param>
        /// <param name="format">format 时间格式 （yyyy-MM-dd HH:mm:ss）</param>
        /// <returns></returns>
        public static String getIntervalFormatDate(long intervalTime, String format)
        {
            if (string.IsNullOrEmpty(format))
                format = "yyyy-MM-dd HH:mm:ss";
            DateTime date = DateTime.Now.AddMinutes(intervalTime);
            return date.ToString(format);
        }

        /// <summary>
        /// 两个时间比较大小
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns> 1：date1晚于date2，-1：date1早于date2，0：传入时间等于当前时间</returns>
        public static int compareDate(DateTime date1, DateTime date2)
        {
            return date1.CompareTo(date2);
        }


        /// <summary>
        /// 两个时间比较大小
        /// </summary>
        /// <param name="dateStr1"></param>
        /// <param name="dateStr2"></param>
        /// <param name="format"></param>
        /// <returns>1：date1晚于date2，-1：date1早于date2，0：传入时间等于当前时间</returns>
        public static int compareDate(String dateStr1, String dateStr2, String format)
        {
            DateTime date1 = DateUtil.StrToDate(dateStr1, format);
            DateTime date2 = DateUtil.StrToDate(dateStr2, format);
            return date1.CompareTo(date2);
        }


        /// <summary>
        /// 获取时间戳
        /// </summary>
        /// <returns></returns>
        public static String getTimestamp()
        {
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }


        /// <summary>
        /// 计算两个日期之间相差的天数
        /// </summary>
        /// <param name="smdate">较小的时间</param>
        /// <param name="bdate">较大的时间</param>
        /// <param name="containEndDate">是否包含结束日期，如果包含，则多加1</param>
        /// <returns>相差天数 @throws ParseException </returns>
        public static int daysBetween(DateTime smdate, DateTime bdate, Boolean containEndDate)
        {
            try
            {
                if (containEndDate) { bdate = bdate.AddDays(1); }
                DateTime start = Convert.ToDateTime(smdate.ToShortDateString());
                DateTime end = Convert.ToDateTime(bdate.ToShortDateString());

                TimeSpan sp = end.Subtract(start);
                return sp.Days;

                //return daysBetween(sdf.format(smdate), sdf.format(bdate), containEndDate);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            return 0;
        }

        /// <summary>
        /// 字符串的日期格式的计算 
        /// </summary>
        /// <param name="smdate"></param>
        /// <param name="bdate"></param>
        /// <param name="containEndDate"></param>
        /// <returns></returns>
        public static int daysBetween(String smdate, String bdate, Boolean containEndDate)
        {
            try
            {
                DateTime smdatet;
                DateTime.TryParse(smdate, out smdatet);
                DateTime bdatet;
                DateTime.TryParse(bdate, out bdatet);

                if (containEndDate) { bdatet = bdatet.AddDays(1); }
                DateTime start = Convert.ToDateTime(smdatet.ToShortDateString());
                DateTime end = Convert.ToDateTime(bdatet.ToShortDateString());

                TimeSpan sp = end.Subtract(start);
                return sp.Days;
            }
            catch (Exception e)
            {
            }
            return 0;
        }

        /// <summary>
        /// 日期向后或者向前推移
        /// </summary>
        /// <param name="dateStr">日期字符串，如："2019-11-11"</param>
        /// <param name="intervalDay">日期推移值；如：1，传入日期参数向后推一天；-1，传入日期参数向前推一天</param>
        /// <returns>返回推移后的日期字符串</returns>
        public static String dayAddOrCut(String dateStr, int intervalDay)
        {
            DateTime dateStrt;
            DateTime.TryParse(dateStr, out dateStrt);
            dateStrt.AddDays(intervalDay);
            return dateStrt.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 时间戳转换成日期格式字符串
        /// </summary>
        /// <param name="seconds">精确到秒的字符串</param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static String timeStamp2Date(String seconds, String format)
        {
            DateTime dateTimeStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(seconds + "0000000");
            TimeSpan toNow = new TimeSpan(lTime);
            return  dateTimeStart.Add(toNow).ToString(format);
        }
    }
}
